package com.huadi.ssm.controller;


import com.huadi.ssm.page.Page;
import com.huadi.ssm.po.Expert;
import com.huadi.ssm.po.Organization;
import com.huadi.ssm.service.ExpertService;
import com.huadi.ssm.service.OrganizationService;
import com.huadi.ssm.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评审专家管理
 *
 */
@RequestMapping("/expert")
@Controller
public class ExpertController {

    @Autowired
    private ExpertService expertService;
    @Autowired
    private OrganizationService organizationService;
    /**
     * 评审专家列表页
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("expert/expert_list");
        List<Organization> organizationList = organizationService.findAll();
        model.addObject("organizationList",organizationList );
        model.addObject("organizationListJson", JSONArray.fromObject(organizationList));
        return model;
    }

    /**
     * 获取评审专家列表
     * @param name
     * @param page
     * @return
     */
    @RequestMapping(value="/get_list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            @RequestParam(value="organizeid",required=false) Long organizeid,
            HttpServletRequest request,
            Page page
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("name", "%"+name+"%");
        Object attribute = request.getSession().getAttribute("userType");
        if("3".equals(attribute.toString())){
            //说明是评审专家
            Expert loginedExpert = (Expert) request.getSession().getAttribute("user");
            queryMap.put("name", "%"+loginedExpert.getName()+"%");
        }
        if(organizeid != null){
            queryMap.put("organizeid", organizeid);
        }
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", expertService.findList(queryMap));
        ret.put("total", expertService.getTotal(queryMap));
        return ret;
    }

    /**
     * 编辑评审专家信息
     * @param expert
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Expert expert){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(expert.getName())){
            ret.put("type", "error");
            ret.put("msg", "评审专家姓名不能为空！");
            return ret;
        }
        if(StringUtils.isEmpty(expert.getPassword())){
            ret.put("type", "error");
            ret.put("msg", "评审专家登录密码不能为空！");
            return ret;
        }
        if(expert.getOrganizeid() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择所属组织机构！");
            return ret;
        }
        expert.setSn(StringUtil.generateSn("E", ""));
        if(expertService.edit(expert) <= 0){
            ret.put("type", "error");
            ret.put("msg", "评审专家修改失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "评审专家修改成功！");
        return ret;
    }


    /**
     * 添加评审专家信息
     * @param expert
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Expert expert){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(expert.getName())){
            ret.put("type", "error");
            ret.put("msg", "评审专家姓名不能为空！");
            return ret;
        }
        if(StringUtils.isEmpty(expert.getPassword())){
            ret.put("type", "error");
            ret.put("msg", "评审专家登录密码不能为空！");
            return ret;
        }
        if(expert.getOrganizeid() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择所属组织机构！");
            return ret;
        }
        expert.setSn(StringUtil.generateSn("E", ""));
        if(expertService.add(expert) <= 0){
            ret.put("type", "error");
            ret.put("msg", "评审专家添加失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "评审专家添加成功！");
        return ret;
    }

    /**
     * 删除评审专家信息
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value="ids[]",required=true) Long[] ids
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(ids == null || ids.length == 0){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的数据！");
            return ret;
        }
        try {
            if(expertService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                ret.put("type", "error");
                ret.put("msg", "删除失败！");
                return ret;
            }
        } catch (Exception e) {
            // TODO: handle exception
            ret.put("type", "error");
            ret.put("msg", "该评审专家下存在其他信息，请勿冲动！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "评审专家删除成功！");
        return ret;
    }
}
